package com.isky.visual.proj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isky.visual.proj.entity.PanelGroupProject;
import com.isky.visual.proj.entity.ProjectShareUserGroup;
import com.isky.visual.proj.mapper.ProjectShareUserGroupMapper;
import com.isky.visual.proj.service.IProjectShareUserGroupService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wenjs
 */
@Service
public class ProjectShareUserGroupServiceImpl extends ServiceImpl<ProjectShareUserGroupMapper, ProjectShareUserGroup> implements IProjectShareUserGroupService {
    @Override
    public boolean doInsert(ProjectShareUserGroup projectShareUserGroup) {

        QueryWrapper<ProjectShareUserGroup> queryObject= new QueryWrapper<>();
        ProjectShareUserGroup p = new ProjectShareUserGroup();
        p.setUserGroupId(projectShareUserGroup.getUserGroupId());
        p.setProjectId(projectShareUserGroup.getProjectId());
        queryObject.setEntity(p);
        List<ProjectShareUserGroup> panelGroupProjects = this.baseMapper.selectList(queryObject);
        if(panelGroupProjects != null && !panelGroupProjects.isEmpty()){
            int i = 0;
            for(ProjectShareUserGroup pgp : panelGroupProjects){
                if (i++ == 0) {
                    if (!pgp.getValid()) {
                        pgp.setValid(true);
                        this.baseMapper.updateById(pgp);
                    }
                    continue;
                }
                this.baseMapper.deleteById(pgp.getId());
            }
            return true;
        }

        return this.save(projectShareUserGroup);
    }

}
